package backTrack;

import java.util.*;

/**
 * 重复选取不一定非得重头开始，从当前位置开始即可
 */

public class ds39 {
    public static void main(String[] args) {
        System.out.println(combinationSum(new int[]{2,3,6,7}, 7));
    }
    public static List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        trackBack(candidates, target, 0, 0,  ans, path);
        return ans;
    }
    public static void trackBack(int[] candidates, int target, int sum, int index, List<List<Integer>> ans, List<Integer> path){
        if(sum > target)
            return;
        if(sum == target){
            ans.add(new ArrayList<>(path));
            return;
        }
        for (int i = index; i < candidates.length; i++) {
            path.add(candidates[i]);
            trackBack(candidates, target, sum + candidates[i], i, ans, path);
            path.remove(path.size() - 1);
        }
    }
}
